package com.sskj.xyft.main;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;

import com.ashokvarma.bottomnavigation.BottomNavigationBar;
import com.gyf.barlibrary.ImmersionBar;
import com.sskj.base.data.User;
import com.sskj.base.utils.APKVersionCodeUtils;
import com.sskj.xyft.AppManager;
import com.sskj.xyft.AppUpDateFragment;
import com.sskj.xyft.BuildConfig;
import com.sskj.xyft.LiveDataBus;
import com.sskj.xyft.MVPActivity;
import com.sskj.xyft.R;
import com.sskj.xyft.market.MarketHomeFragment;
import com.sskj.xyft.market.bean.MarketData;
import com.sskj.xyft.mine.MineFragment;
import com.sskj.xyft.mine.data.AppVersionBean;
import com.sskj.xyft.optional.OptionalFragment;
import com.sskj.xyft.transaction.TransactionHomeFragment;
import com.sskj.xyft.view.NavigationBar;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import sskj.lee.appupdatelibrary.BaseUpdateDialogFragment;
import sskj.lee.appupdatelibrary.BaseVersion;

public class MainActivity extends MVPActivity<MainPresenter> {
    @BindView(R.id.navigationBar)
    NavigationBar navigationBar;

    private List<Fragment> fragments = new ArrayList<>();

    public MarketData currentStock;

    @Override
    public int getLayoutId() {
        return R.layout.activity_main;
    }

    @Override
    public MainPresenter getPresenter() {
        return new MainPresenter();
    }

    @Override
    public void initView() {
        fragments.add(MarketHomeFragment.newInstance());
//        fragments.add(MessageHomeFragment.newInstance());  资讯
        fragments.add(OptionalFragment.newInstance());
        fragments.add(TransactionHomeFragment.newInstance());
        fragments.add(MineFragment.newInstance());
        ImmersionBar.with(this).navigationBarColor(R.color.color_999999).init();
    }

    @Override
    protected void onResume() {
        super.onResume();
        if (isLogin) {
            mPresenter.getAccountDetail(account, false);
        }
    }

    @Override
    protected void onNewIntent(Intent intent) {
        super.onNewIntent(intent);
        int index = intent.getIntExtra("index", -1);

        if (index == 1) {
//            navigationBar.selectTab(index);
//            int childIndex = intent.getIntExtra("childIndex", 0);
//            MessageHomeFragment fragment = (MessageHomeFragment) fragments.get(index);
//            fragment.setIndex(childIndex);
        }
        if (index == 2) {
            navigationBar.selectTab(index);
            int childIndex = intent.getIntExtra("childIndex", 0);
            MarketData data = (MarketData) intent.getSerializableExtra("marketData");
            TransactionHomeFragment fragment = (TransactionHomeFragment) fragments.get(index);
            fragment.setIndex(childIndex, data);
        }

    }


    public static void startToIndex(Context context, int index, int childIndex, MarketData data) {
        Intent intent = new Intent(context, MainActivity.class);
        intent.putExtra("index", index);
        intent.putExtra("childIndex", childIndex);
        intent.putExtra("marketData", data);
        context.startActivity(intent);
    }

    @Override
    public void initData() {
        mPresenter.checkVersion(BuildConfig.VERSION_NAME);
        initFragment();
        setTabSelection(0);
        navigationBar.setTabSelectedListener(new BottomNavigationBar.OnTabSelectedListener() {
            @Override
            public void onTabSelected(int position) {
                setTabSelection(position);
            }

            @Override
            public void onTabUnselected(int position) {
            }

            @Override
            public void onTabReselected(int position) {
            }
        });
    }

    public void initFragment() {
        FragmentManager manager = getSupportFragmentManager();
        FragmentTransaction transaction = manager.beginTransaction();
        for (Fragment fragment : fragments) {
            transaction.add(R.id.main_content, fragment);
        }
        transaction.show(fragments.get(2));
        transaction.commit();
    }


    public void setTabSelection(int index) {
        // 开启一个Fragment事务
        FragmentTransaction fragmentTransaction = getSupportFragmentManager().beginTransaction();
        // 先隐藏掉所有的Fragment，以防止有多个Fragment显示在界面上的情况
        hideFragments(fragmentTransaction);
        fragmentTransaction.show(fragments.get(index));
        fragmentTransaction.commitAllowingStateLoss();

    }

    /**
     * 将所有的Fragment都置为隐藏状态。
     *
     * @param fragmentTransaction 用于对Fragment执行操作的事务
     */
    private void hideFragments(FragmentTransaction fragmentTransaction) {
        for (Fragment fragment : fragments) {
            fragmentTransaction.hide(fragment);
        }
    }

    public void checkVersion(AppVersionBean data) {
        if (data == null) {
            return;
        }
        if (!this.getClass().getName().equals(AppManager.getInstance().getCurrentActivity().getClass().getName())) {
            return;
        }
        String verName = APKVersionCodeUtils.getVerName(this);
        data.setViewStyle(BaseVersion.NOTIFYCATION_STYLE);
        if (APKVersionCodeUtils.compareVersion(data.getAppVersion(), verName) == 1) {
            android.app.FragmentTransaction fragmentTransaction = getFragmentManager().beginTransaction();
            getFragmentManager().beginTransaction();
            AppUpDateFragment appUpDateFragment = new AppUpDateFragment();//自定义UI
            Bundle bundle = new Bundle();
            bundle.putSerializable(BaseUpdateDialogFragment.INTENT_KEY, data);
            appUpDateFragment.setArguments(bundle);
            appUpDateFragment.show(fragmentTransaction, "update");
        }
    }

    public void setAccountInfo(User data) {
        LiveDataBus.get().with(1,User.class).postValue(data);

//        userViewModel.setUser(this, data);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
    }
}
